<?php
class ArrayTool
{
	/**
	 * 取得层次机构
	 * Enter description here ...
	 * @param unknown_type $array
	 */
    static function ArrayToNodes($array,$id=0) {
        $ArrayReturn = array();
        foreach($array as $item){
            if($item["id"] == $id){
                $ArrayReturn = ArrayTool::ArrayGoNodes($array,$id);
                return $ArrayReturn;
            }
        }

    	return $ArrayReturn;
    }

    /**
     * 递归调用
     * Enter description here ...
     * @param unknown_type $arrayAll
     * @param unknown_type $return
     * @param unknown_type $parent
     */
    static function ArrayGoNodes($arrayAll,$parent){
    	$rstempTwo = array();
    	foreach ($arrayAll as $key=>$item){
	    	if($item['parent'] == $parent){
	    		$rstempOne = array();
	    		foreach ($item as $key=>$val){
	    			$rstempOne[$key] = $val;
	    		}
	    		$data = ArrayTool::ArrayGoNodes($arrayAll,$item['id']);
	    		if(count($data)>0){
	    			$rstempOne['children'] = $data;
	    		}
	    		$rstempTwo[]=$rstempOne;
	    	}
	    }
    	
    	return $rstempTwo;
    }

    /**
     * 取得下属ID
     * Enter description here ...
     * @param unknown_type $array
     */
    static function ArrayToIDs($array,$id=0) {
        $ArrayReturn = ArrayTool::ArrayGoIDs($array,$id);
        $ArrayReturn[] = $id;
        sort($ArrayReturn);
        $Return = implode(',',$ArrayReturn);
        return $Return;
    }

    /**
     * 递归调用
     * Enter description here ...
     * @param unknown_type $arrayAll
     * @param unknown_type $return
     * @param unknown_type $parent
     */
    static function ArrayGoIDs($arrayAll,$parent){
        $rstempTwo = array();
        foreach ($arrayAll as $key=>$item){
            if($item['parent'] == $parent){
                $rstempOne = array();
                $data = ArrayTool::ArrayGoIDs($arrayAll,$item['id']);
                if(count($data) > 0){
                    $rstempOne = $data;
                    $rstempTwo = array_merge($rstempOne, $rstempTwo);
                }
                $rstempTwo[]=$item['id'];
            }
        }

        return $rstempTwo;
    }
    
	/**
     * 取得上属ID
     * Enter description here ...
     * @param unknown_type $array
     */
    static function ArrayToParentIDs($array,$id=0) {
        $ArrayReturn = ArrayTool::ArrayGoParentIDs($array,$id);
        if(count($ArrayReturn)==0){
        	$ArrayReturn[] = "0";
        }
        return $ArrayReturn;
    }

    /**
     * 递归调用
     * Enter description here ...
     * @param unknown_type $arrayAll
     * @param unknown_type $return
     * @param unknown_type $parent
     */
    static function ArrayGoParentIDs($arrayAll,$id){
        $rstempTwo = array();
        foreach ($arrayAll as $key=>$item){
            if($item['id'] == $id){
                $rstempOne = array();
                $data = ArrayTool::ArrayGoParentIDs($arrayAll,$item['parent']);
                if(count($data) > 0){
                    $rstempOne = $data;
                    $rstempTwo = array_merge($rstempOne, $rstempTwo);
                }
                if($item['status']=='是'){
                	$rstempTwo[]=$item['id'];
                }
            }
        }

        return $rstempTwo;
    }

    /**
     * 取得层次机构
     * Enter description here ...
     * @param unknown_type $array
     */
    static function ArrayToNode($array) {
        $return = array();
        $ArrayReturn = ArrayTool::ArrayGoNode($array,$return,0);
        return $ArrayReturn;
    }

    /**
     * 递归调用
     * Enter description here ...
     * @param unknown_type $arrayAll
     * @param unknown_type $return
     * @param unknown_type $parent
     */
    static function ArrayGoNode($arrayAll,$return,$parent){
        $rstempTwo = array();
        foreach ($arrayAll as $key=>$item){
            if($item['parent'] == $parent){
                $rstempOne = array();
                foreach ($item as $key=>$val){
                    $rstempOne[$key] = $val;
                }
                $data = ArrayTool::ArrayGoNode($arrayAll,$return,$item['id']);
                if(count($data)>0){
                    $rstempOne['children'] = $data;
                }
                $rstempTwo[]=$rstempOne;
            }
        }

        return $rstempTwo;
    }
}